function toggleCheckboxes(checkboxes, isChecked)
{
	if (checkboxes != null)
	{
		if (checkboxes.length != undefined)
		{
			var numCheckboxes = checkboxes.length;
			for(index = 0; index < numCheckboxes; index++)
			{
				if(!checkboxes[index].disabled)
				{
					checkboxes[index].checked = isChecked;
				}			
			}
		} 
		else
		{
			if(checkboxes.disabled != undefined && !checkboxes.disabled)
			{
				checkboxes.checked = isChecked;
			}
		}
	}
}

function toggleMasterCheckbox(masterCheckbox, isChecked, checkboxes)
{
	if (isChecked == false)
	{
		masterCheckbox.checked = false;
	}
	else if (masterCheckbox.checked == false)
	{
		if (checkboxes.length != undefined)
		{
			var numCheckboxes = checkboxes.length;
			var allChecked = true;
		
			for (index = 0; index < numCheckboxes && allChecked; index++)
			{
				if (!checkboxes[index].disabled && checkboxes[index].checked == false)
				{
					allChecked = false;
				}
			}	
			if (allChecked)
			{
				masterCheckbox.checked = true;
			}
		}
		else
		{
			masterCheckbox.checked = isChecked;
		}		
	}
}


function getCourseCategory(categoryField)
{
		var category = document.getElementsByName(categoryField);
		
		if(category[0].length > 1) 
		{			
			return;		
		}		
	
		$.ajax({
			type: "post",
			url: "/qiyexuexi/admin/getCourseCategory.do?method=getCourseCategory",
			beforeSend: function(XMLHttpRequest){
			},
			success: function(data, textStatus) {		
				
				//[111, 111, 111, 111]	
				//var startIndex = data.indexOf("[");
				var endIndex = data.indexOf("]");
				
				var subString = data.substr(1, endIndex-1);
				var arr = subString.split(",");

				//字符串前面的空格需要去掉
				for(var i = 0; i < arr.length; i++) {
					while(arr[i].indexOf(" ") == 0 && (arr[i].length > 1)) {
						arr[i] = arr[i].substring(1, arr[i].length);
					}
					category[0].options.add(new Option(arr[i], arr[i]));														
				}
			},
			complete: function(XMLHttpRequest, textStatus){
				//HideLoading();
			},
			error: function(){
				alert("failure");
			}							
		});
}


function getTeacherList(contextPath, category)
{
	var url = contextPath + "/teacher/viewAllTeacher.do?method=viewAllTeacher&category=" + category;
	url = encodeURI(url);
	url = encodeURI(url);		
	window.location=url;
}



var validateUserId;
function assignValidateUserId(value)
{
	validateUserId = value;	
}
function checkUserId(value)
{	
	if(value == '')
	{
		$("#userIdRule")[0].innerHTML = "请输入用户名";
		$("#userIdRule")[0].style.display = 'block';
		assignValidateUserId('false');
		return;
	}
	$.ajax({
		type: "post",
		url: "/qiyexuexi/checkUserId.do?method=checkUserId",
		data:{"userId":value},
		beforeSend: function(XMLHttpRequest){
		},
		success: function(data, textStatus) {		
			if(data == 'true')
			{	
				$("#userIdRule")[0].style.display = 'none';
				$("#checkIdOK")[0].style.display = 'block';
				assignValidateUserId('true');
			}
			else 
			{	
				$("#checkIdOK")[0].style.display = 'none';
				$("#userIdRule")[0].innerHTML = "该用户名已存在";
				$("#userIdRule")[0].style.display = 'block';	
				assignValidateUserId('false');
			}
		},
		complete: function(XMLHttpRequest, textStatus){
			//HideLoading();
		},
		error: function(){
			alert("failure");
		}
	});
}


var validatePwd;
function assignValidatePwd(value)
{
	validatePwd = value;	
}
function checkPwd(pwd)
{
	$("#checkPwdOK")[0].style.display = 'none';
	$("#passwordRule")[0].style.display = 'none';
	if(pwd == '')
	{
		$("#passwordRule")[0].innerHTML = "请输入6-16位数字、字母的密码";
		$("#passwordRule")[0].style.display = 'block';
		assignValidatePwd('false');
	}
	else if(pwd.length<6 || pwd.length>16)
	{
		$("#passwordRule")[0].innerHTML = "密码不符合规定";
		$("#passwordRule")[0].style.display = 'block';
		assignValidatePwd('false'); 
	}
	else 
	{
		$("#checkPwdOK")[0].style.display = 'block';
		assignValidatePwd('true');
	}
}

var validateConfirmPwd;
function assignValidateConfirmPwd(value)
{
	validateConfirmPwd = value;	
}
function confirmPwd(confirmPwd)
{
	$("#confirmPwdRule")[0].style.display = 'none';
	$("#confirmPwdOK")[0].style.display = 'none';
	if(confirmPwd == '')
	{
		$("#confirmPwdRule")[0].innerHTML = "请再输入一次密码";
		$("#confirmPwdRule")[0].style.display = 'block';
		assignValidateConfirmPwd('false');
	} 
	else 				
	{
		var pwd = document.getElementsByName("user.password")[0].value;
		if(pwd != confirmPwd)
		{
			$("#confirmPwdRule")[0].innerHTML = "密码不一致";
			$("#confirmPwdRule")[0].style.display = 'block';
			assignValidateConfirmPwd('false');
		}
		else if(confirmPwd.length<6 || confirmPwd.length>16)
		{
			$("#confirmPwdRule")[0].innerHTML = "密码不符合规定";
			$("#confirmPwdRule")[0].style.display = 'block';
			assignValidateConfirmPwd('false');
		} 
		else 
		{
			$("#confirmPwdOK")[0].style.display = 'block';
			assignValidateConfirmPwd('true');
		}
	}
}

var checkCodeResult;
function assignCheckCode(value)
{
	checkCodeResult = value;	
}

function checkCode(code)
{	
	if(checkCodeResult == 'true')
	{
		return;
	}
	$("#validateCode")[0].style.display = 'none';
	$("#codeOK")[0].style.display = 'none';
	if(code == '')
	{
		$("#validateCode")[0].innerHTML = "请输入验证码!"
		$("#validateCode")[0].style.display = 'block';
		assignCheckCode('false')
		return;
	}
	$.ajax({
		type: "post",
		url: "/qiyexuexi/validateCode.do?method=validateCode",
		data:{"code":code},
		beforeSend: function(XMLHttpRequest){
		},
		success: function(data, textStatus) {		
			if(data == 'true')
			{
				$("#validateCode")[0].style.display = 'none';
				$("#codeOK")[0].style.display = 'block';
				assignCheckCode('true')
			}
			else
			{	
				$("#validateCode")[0].innerHTML = "验证码输入有误!"
				$("#validateCode")[0].style.display = 'block';
				assignCheckCode('false')
			}
		},
		complete: function(XMLHttpRequest, textStatus){
		},
		error: function(){
			alert("failure");
		}
	});
}


function login(contextPath)
{
	var userName = $("#userName")[0].value;
	var pwd = $("#password")[0].value;
	
	if(userName == '' || pwd == '')
	{
		removeValidate("loginTable");
		removeFail("loginTable");
		createValidate("loginTable");
		
		return;
	}
	
	$("#loginDiv")[0].style.display = 'none';
	$("#busy")[0].style.display = 'block';
	
	$.ajax({
		type: "post",
		url: contextPath + "/login.do?method=login",
		
		data:{"userName":userName, "password":pwd},
		beforeSend: function(XMLHttpRequest){
		},
		success: function(data, textStatus) {
			if(data == 'true') {
				$("#busy")[0].style.display = 'none';
				$("#loginSuccess")[0].style.display = 'block';
				
				$("#1")[0].innerHTML = $("#1")[0].innerHTML + userName;
				
				var prefix = "<a class='normal_link' style='color:#3166CC;' href=" + contextPath + "/mypage.do?method=mypage>";
				var postfix = "</a>";
				$("#2")[0].innerHTML = prefix+$("#2")[0].innerHTML+postfix;
			} else {
				$("#loginDiv")[0].style.display = 'block';
				$("#busy")[0].style.display = 'none';
				
				removeValidate("loginTable");
				removeFail("loginTable");
				createFail("loginTable");
			}		
		},
		complete: function(XMLHttpRequest, textStatus){
		},
		error: function(){
			alert("登录失败");
		}
	});
}

function removeValidate(tableId)
{
	var tbody =  document.getElementById(tableId).getElementsByTagName("TBODY")[0];
	if(document.getElementById("validate") != null)
	{
		tbody.removeChild(document.getElementById("validate"));
	}
}

function removeFail(tableId)
{
	var tbody =  document.getElementById(tableId).getElementsByTagName("TBODY")[0];
	if(document.getElementById("fail") != null)
	{
		tbody.removeChild(document.getElementById("fail"));
	}
}
function createValidate(tableId)
{
	var tbody =  document.getElementById(tableId).getElementsByTagName("TBODY")[0];
	var tr = document.createElement("TR");
	tr.id = "validate";
	var td1 = document.createElement("TD");
	td1.height = "10";
	td1.innerHTML = "请输入用户名/密码.";
	td1.style.color = "red";
	td1.colSpan = "2";
	td1.align = "center";
	tr.appendChild(td1);
	tbody.appendChild(tr);
}
function createFail(tableId)
{
	var tbody =  document.getElementById(tableId).getElementsByTagName("TBODY")[0];
	var tr = document.createElement("TR");
	tr.id = "validate";
	var td1 = document.createElement("TD");
	td1.height = "10";
	td1.innerHTML = "用户命/密码不正确.";
	td1.style.color = "red";
	td1.colSpan = "2";
	td1.align = "center";
	tr.appendChild(td1);
	tbody.appendChild(tr);
}
function logout(contextPath)
{
	window.location = contextPath + "/logout.do?method=logout";
}

function subscribeCourse(contextPath, courseId)
{
	$.ajax({
		type: "post",
		url: contextPath + "/subscribeCourse.do?method=subscribeCourse",
		data:{"courseId": courseId},
		beforeSend: function(XMLHttpRequest){
		},
		success: function(data, textStatus) {
			if(data == 'true') {
				alert("收藏成功!");
				$("#subscribe")[0].style.color = "#1E50A2";
				$("#subscribe")[0].innerHTML = "【该课程已收藏】";
			}else {
				alert("收藏失败!");
			}
		},
		complete: function(XMLHttpRequest, textStatus){
		},
		error: function(){
			alert("收藏失败");
		}
	});
}